﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;

namespace PRE.catalog
{
    public partial class FrmKhachHang : PRE.catalog.FrmBase
    {
        DAL.KhachHangDAL _dal = new DAL.KhachHangDAL();
        public FrmKhachHang()
        {
            InitializeComponent();
        }

        #region Override

        /// <summary>
        /// Perform when click add button
        /// </summary>
        protected override void PerformAdd()
        {

            ReadOnlyControl(false);
            ClearDataBindings();
            ChangeStatus(false);

            txtMaKH.ResetText();

            txtTenKH.ResetText();
            txtDiaChi.ResetText();
            txtSDT.ResetText();
            dtengaysinh.EditValue = DateTime.Now;
            txtCMND.ResetText();
            
            txtMaKH.Focus();

            base.PerformAdd();
        }

        /// <summary>
        /// Perform when click cancel button
        /// </summary>
        protected override void PerformCancel()
        {
            ChangeStatus();
            PerformRefresh();
            base.PerformCancel();
        }

        /// <summary>
        /// Perform when click delete button
        /// </summary>
        /// <returns></returns>
        protected override void PerformDelete()
        {
            string id = txtMaKH.Text.Trim();

            DialogResult result;
            var ok = _dal.Delete(id);
            result = MessageBox.Show("Bạn có chắc xóa không ? ", "Thông Báo", MessageBoxButtons.OKCancel, MessageBoxIcon.Warning);
            if (result == DialogResult.OK)
            {
                if (ok)
                {
                    PerformRefresh();
                }
                else
                    MessageBox.Show("Lỗi! Không xóa được", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }

            base.PerformDelete();
        }

        /// <summary>
        /// Perform when click edit button
        /// </summary>
        protected override void PerformEdit()
        {
            ReadOnlyControl(false);
            ChangeStatus(false);
            txtMaKH.Focus();
            base.PerformEdit();
        }

        /// <summary>
        /// Perform when click print button
        /// </summary>
        protected override void PerformPrint()
        {
            base.PerformPrint();
        }

        /// <summary>
        /// Load data or perform when click refresh button
        /// </summary>
        protected override void PerformRefresh()
        {
            ReadOnlyControl();
            LoadData();

            if (_dtb != null)
            {
                // Binding data
                ClearDataBindings();
                if (_dtb.Rows.Count > 0) DataBindingControl();
            }

            base.PerformRefresh();
        }

        /// <summary>
        /// Perform when click save button
        /// </summary>
        protected override void PerformSave()
        {
          
            var tenkh = Proper(txtTenKH.Text);
            var address = Proper(txtDiaChi.Text);

            if (IsAdd)
            {
                var o = new DAL.Entities.Khach_Hang()
                {
                    ma_kh = txtMaKH.Text,

                    ten_kh = tenkh,
                    namsinh_kh = Convert.ToDateTime(dtengaysinh.EditValue),
                    cmnd_kh = txtCMND.Text,
                    sdt_kh = txtSDT.Text,
                    diachi_kh = address,
                };

                var oki = _dal.Insert(o);
                if (oki)
                {
                    //MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus(false);
                    PerformAdd();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }

            }

            if (IsEdit)
            {
                var _makh = txtMaKH.Text.Trim();

                var o = new DAL.Entities.Khach_Hang()
                {
                    ma_kh = _makh,

                    ten_kh = tenkh,
                    namsinh_kh = Convert.ToDateTime(dtengaysinh.EditValue),
                    cmnd_kh = txtCMND.Text,
                    sdt_kh = txtSDT.Text,
                    diachi_kh = address,
                };

                var oki = _dal.Update(o);
                if (oki)
                {
                    // MessageBox.Show("Đã lưu thành công", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    PerformRefresh();
                    ChangeStatus();
                }
                else
                {
                    MessageBox.Show("Lỗi! Lưu thất bại", "Thông Báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
                    ChangeStatus(false);
                    ReadOnlyControl(false);
                }
            }


            base.PerformSave();
        }

        /// <summary>
        /// Reset all input control
        /// </summary>
        protected override void ResetText()
        {

            txtDiaChi.Text = null;
            txtSDT.Text = null;

            dtengaysinh.EditValue = DateTime.Now;
            txtCMND.Text = null;
            txtTenKH.Text = null;
            txtMaKH.Text = null;
            base.ResetText();
        }

        /// <summary>
        /// Clear data binding
        /// </summary>
        protected override void ClearDataBindings()
        {
            txtMaKH.DataBindings.Clear();

            dtengaysinh.DataBindings.Clear();
            txtDiaChi.DataBindings.Clear();
            txtSDT.DataBindings.Clear();
            txtCMND.DataBindings.Clear();
            txtTenKH.DataBindings.Clear();

            base.ClearDataBindings();
        }

        /// <summary>
        /// Add data binding
        /// </summary>
        protected override void DataBindingControl()
        {
            txtMaKH.DataBindings.Add("EditValue", _dtb, ".Ma_KH");

            txtTenKH.DataBindings.Add("EditValue", _dtb, ".Ten_KH");
            dtengaysinh.DataBindings.Add("EditValue", _dtb, ".NgaySinh");
            txtCMND.DataBindings.Add("EditValue", _dtb, ".CMND");
            txtSDT.DataBindings.Add("EditValue", _dtb, ".SDT");
            txtDiaChi.DataBindings.Add("EditValue", _dtb, ".DiaChi");
            

            base.DataBindingControl();
        }

        /// <summary>
        /// Set read only control on form
        /// </summary>
        /// <param name="isReadOnly">Read only is trule else normal</param>
        protected override void ReadOnlyControl(bool isReadOnly = true)
        {
            txtMaKH.Properties.ReadOnly = isReadOnly;

            dtengaysinh.Properties.ReadOnly = isReadOnly;
            txtTenKH.Properties.ReadOnly = isReadOnly;
            txtDiaChi.Properties.ReadOnly = isReadOnly;
            txtSDT.Properties.ReadOnly = isReadOnly;
            txtCMND.Properties.ReadOnly = isReadOnly;
            

            grcmain.Enabled = isReadOnly;

            base.ReadOnlyControl(isReadOnly);
        }

        protected override void PerformSearch()
        {
            base.PerformSearch();
        }

        /// <summary>
        /// Update object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool UpdateObject()
        {
            return base.UpdateObject();
        }

        /// <summary>
        /// Insert object
        /// </summary>
        /// <returns>True if successful else false</returns>
        protected override bool InsertObject()
        {
            return base.InsertObject();
        }

        /// <summary>
        /// Load data
        /// </summary>
        protected override void LoadData()
        {
            //DataTable tmp = _dal3.Select_Current(DateTime.Now);
            //string _makhoa = tmp.Rows[0]["Makhoa"] + "";
            _dtb = _dal.Select();


            if (_dtb != null)
            {
                grcmain.DataSource = _dtb;
            }

            base.LoadData();
        }
        #endregion

        private void FrmKhachHang_Load(object sender, EventArgs e)
        {
            ChangeStatus();
            PerformRefresh();
        }
    }
}
